package org.snia.cdmi.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlRootElement;

/**
 * Represents an container.
 */
@XmlRootElement(name = "DataObject")
public class MetaDataObject extends DataObjectWithAcl {
	private static final long serialVersionUID = -8646831898339939580L;

	/** 用户的元数据信息 */
	private Map<String, String> metadata = null;

	/** 当前版本 */
	private String currentVersionId;

	/** 文件的版本信息 */
	private List<DataObjectVersion> versions;

	// /** 当前最新版本号 */
	// private DataObjectVersion currentVersion;

	/**
	 * Create a container without any name specified
	 */
	public MetaDataObject() {
	}

	public Map<String, String> getUserMetadata() {
		if (metadata == null) {
			metadata = new HashMap<String, String>();
		}
		return metadata;
	}

	public void setUserMetadata(Map<String, String> metadata) {
		if (this.metadata == null) {
			this.metadata = new HashMap<String, String>();
		}
		this.metadata.putAll(metadata);
	}

	/**
	 * @param versions
	 *            the versions to set
	 */
	public void setVersions(List<DataObjectVersion> versions) {
		this.versions = versions;
	}

	/**
	 * @return the versions
	 */
	public List<DataObjectVersion> getVersions() {
		return versions;
	}

	public DataObjectVersion newVersion() {
		return new DataObjectVersion();
	}

	/**
	 * 添加一个新的版本
	 */
	public void setNewVersion(String uuid) {
		if (this.versions == null) {
			this.versions = new ArrayList<DataObjectVersion>();
		}
		DataObjectVersion dv = new DataObjectVersion();
		String newVersionId = uuid + "_" + String.valueOf(new Date().getTime());
		dv.setVersionId(newVersionId);
		this.versions.add(dv);
		this.currentVersionId = dv.getVersionId();
	}

	/**
	 * 向当前版本中添加一个新的分片
	 * 
	 * @param md5
	 * @param uuid
	 */
	public void setNewSlice(String md5) {
		// 获得当前版本
		DataObjectVersion dv = this.versions.get(this.versions.size() - 1);
		dv.setNewSlice(md5);
	}

	/**
	 * 新增一个新的版本
	 * 
	 * @param versionId
	 * @param md5
	 * @param uuid
	 */
	public void setNewVersion(String versionId, String md5) {
		DataObjectVersion dv = new DataObjectVersion();
		if (versionId != null) {
			dv.setVersionId(versionId);
		} else {
			dv.setVersionId(String.valueOf(new Date().getTime()));
		}

		dv.setNewSlice(md5);
		this.versions.add(dv);
	}

	/**
	 * 删除一个指定版本
	 */
	public void deleteVersion(String versionId) {
		// 文件如果只有一个版本，只能删除文件，而不能删除这个版本
		if (this.versions == null || this.versions.size() < 1) {
			return;
		}
		if (versionId != null) {
			for (DataObjectVersion dv : this.versions) {
				if (dv.getVersionId().equals(versionId)) {
					this.versions.remove(dv);
				}
			}
		} else {
			// 删除最老版本
			this.versions.remove(0);
		}
	}

	/**
	 * 获得当前最新版本的版本号
	 * 
	 * @return
	 */
	public String getCurrentVersionId() {
		if (this.currentVersionId == null) {
			if (this.versions != null && this.versions.size() > 0) {
				DataObjectVersion dv = this.versions
						.get(this.versions.size() - 1);
				return dv.getVersionId();
			}
			return null;
		}
		return this.currentVersionId;
	}

	/**
	 * 获得当前最老的版本的版本号
	 * 
	 * @return
	 */
	public DataObjectVersion getOldestVersion() {
		if (this.versions != null && this.versions.size() > 0) {
			return this.versions.get(0);
		}
		return null;
	}
}
